<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;
use app\erp\model\ErpSupplier;
use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
/**
 * 团购管理
 * @package app\erp\model
 */
class GroupSponsorlogic{
    /**
     * 团购列表上轮播
     * @author  shqiang
     * @return array_out(code, message, data)
     */
    public function group_banner($user_id, $store_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id' => $user_id,
                'store_id' => $store_id,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $map = [];
            $map[] = ['store_id', 'in', [0,$store_id]];
			$map[] = ['banner_status', '=', 1];
            $store_banner = model('ErpGroupBanner') -> where($map) -> order('sort asc, store_id desc,create_time asc') -> select();
            $return_list = [];
            foreach($store_banner as $value){
                $return_list[] = [
                    'bannerId' 	=> $value['id'],
                    'imgUrl' 	=> $value['img_url'],
                    'typeId'	=> $value['type_id'],
                ];
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success', $return_list);
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 团购商品列表
     * @author  shqiang
     * @return array_out(code, message, data)
     */
    public function group_sponsor_sku_list($user_id, $store_id, $tg_status = 1){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' => $user_id,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $result_list = $this -> group_sponsor_sku_info($user_id, $store_id, $tg_status);
            $result_list = $result_list['data'];

            $page       = input('page');
            $page_size  = input('page_size');

            $count = count($result_list);//总条数
            $start=($page-1)*$page_size;//偏移量，当前页-1乘以每页显示条数
            $return_list = array_slice($result_list, $start, $page_size);
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $return_list);
            /********逻辑返回********/

        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 团购商品详情
     * @author  shqiang
     * @return array_out(code, message, data)
     */
    public function group_sponsor_sku_details($user_id, $store_id, $tg_status = 5, $sponsor_id = 0, $group_sid = 0){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id' => $user_id,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $result_list = $this -> group_sponsor_sku_info($user_id, $store_id, $tg_status, $sponsor_id, $group_sid);
            $relust = [];
            if(!empty($result_list['data'])){
                $relust = $result_list['data'][0];
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success', (object)$relust);
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 团购详情
     * @author  shqiang
     * @return array_out(code, message, data)
     */
    public function group_sponsor_sku_info($user_id, $store_id, $tg_status, $sponsor_id = 0, $group_sid = 0){
        $map = [];
        $map[] = ['sp_status', '=', 1];
        $map[] = ['store_id', 'in', [0, $store_id]];
        if($sponsor_id > 0) $map[] = ['id', '=', $sponsor_id];
        $list = model('ErpGroupSponsorSku') -> where($map) -> order('sorts asc, id desc') -> select();

        $result_list = [];
        foreach($list as $key => $value){
            $map = [];
            $map['id']          = $value['group_sid'];
            $map['tg_status']   = 1;
            $group_info = model('ErpGroupSponsor') -> where($map) -> find(); //团购规则
            if(empty($group_info)) continue; //团购是否开启
            $rel_list = [];
            $rel_list['groupSponsorInfo'] = [];
            $ksy_num = $value['ksy_num'];
            if($sponsor_id > 0){
                //商品详情
                $goods_sku  = controller('GoodsSkulogic', 'logic') -> goods_sku_info_skuid($user_id, $value['sku_id'], $store_id);
                $goodsSkuInfo = $goods_sku['data'];
                //缩略图
                $coverlistimgArr = [];
                $coverlist = model('ErpGroupSponsorSkuImgs') -> where([['group_skuid', '=', $value['id']],['type', '=', 1]]) -> column('id,img_url,width,height');
                if(!empty($coverlist)){
                    unset($goodsSkuInfo['goodsInfo']['coverImgArr']);
                    foreach($coverlist as $k => $v){
                        $coverlistimgArr[] = [
                            'id' => $k,
                            'img_url'   => $v['img_url'],
                            'width'     => $v['width'],
                            'height'    => $v['height'],
                        ];
                    }
                    $goodsSkuInfo['goodsInfo']['coverImgArr'] = $coverlistimgArr;
                }
                $detailsImgArr = [];
                $detailslist = model('ErpGroupSponsorSkuImgs') -> where([['group_skuid', '=', $value['id']],['type', '=', 2]]) -> column('id,img_url,width,height');
                if(!empty($detailslist)){
                    unset($goodsSkuInfo['goodsInfo']['detailsImgArr']);
                    foreach($detailslist as $k => $v){
                        $detailsImgArr[] = [
                            'id' => $k,
                            'img_url'   => $v['img_url'],
                            'width'     => $v['width'],
                            'height'    => $v['height'],
                        ];
                    }
                    $goodsSkuInfo['goodsInfo']['detailsImgArr'] = $detailsImgArr;
                }
                if($value['ksy_num'] > $goodsSkuInfo['kucunjiage'] -> available_num){
                    //判断当前门店 数量与团购数量
                    $ksy_num = $goodsSkuInfo['kucunjiage'] -> available_num;
                }

                $rel_list['goodsSkuInfo'] = $goodsSkuInfo;
                //团购详情查团购人
                $sponsor_user_info = $this -> group_sponsor_sku_userinfo($user_id, $store_id, $sponsor_id, $value['sku_id']);
                $rel_list['groupSponsorInfo']   = $sponsor_user_info['data'];
            }else{
                //团购列表用
                $goods_sku = model('ErpGoodsSku') -> find($value['sku_id']);
                $coverlistimgArr = [];
                $coverlist = model('ErpGroupSponsorSkuImgs') -> where([['group_skuid', '=', $value['id']],['type', '=', 1]]) -> column('id,img_url,width,height');
                if(!empty($coverlist)){
                    foreach($coverlist as $k => $v){
                        $coverlistimgArr[] = [
                            'id' => $k,
                            'img_url'   => $v['img_url'],
                            'width'     => $v['width'],
                            'height'    => $v['height'],
                        ];
                    }
                }else{
                    $coverlist = controller('erp/GoodsCoverimglogic', 'logic') -> getCoverImgListByGoodsid($goods_sku['goods_id']);//缩略图
                    if($coverlist['code'] == 200) $coverlistimgArr = $coverlist['data'];
                }

                $sku_price = model('ErpGoodsWarehouseStockNum') -> get_goods_stock_price($store_id, $value['sku_id']);
                $price = $sku_price['data'] > 0 ? $sku_price['data'] : $goods_sku['price'];

                $goods_sku_info = [
                    'goodsName' => $goods_sku['goods_name'],
                    'goodsSpecifications' => $goods_sku['goods_specifications'],
                    'price' => $price,
                    'coverImgArr' => $coverlistimgArr,
                ];
                $rel_list['groupSponsorInfo']   = [];
                $rel_list['goodsSkuInfo']       = (object)$goods_sku_info;
            }
            $rel_list['groupSponsorInfo']['groupSid']           = (int)$value['group_sid'];
            $rel_list['groupSponsorInfo']['sponsorId']          = (int)$value['id'];
            $rel_list['groupSponsorInfo']['showPrice']          = (string)$value['show_price'];
            $rel_list['groupSponsorInfo']['limitNum']           = (int)$value['limit_num'];
            $rel_list['groupSponsorInfo']['ksyNum']             = (int)$ksy_num;
            $rel_list['groupSponsorInfo']['DjNum']              = (int)$value['dj_num'];
            $rel_list['groupSponsorInfo']['tgTitle']            = (string)$group_info['tg_title'];
            $rel_list['groupSponsorInfo']['tgJoinNum']          = (int)$group_info['tg_join_num'];
            $rel_list['groupSponsorInfo']['tgStartTime']        = (int)$group_info['tg_start_time'];
            $rel_list['groupSponsorInfo']['tgEndTime']          = (int)$group_info['tg_end_time'];
			
			if($tg_status == 5 && count($result_list) >= 12) continue;
			
            if(($tg_status == 1 or $tg_status == 5) && $group_info['tg_start_time'] <= time() && $group_info['tg_end_time'] > time()){ //正在开团
                if($value['ksy_num'] > 0){ //未售完
                    $result_list[] = $rel_list;
                }
            }
            if(($tg_status == 2 or $tg_status == 5) && $group_info['tg_start_time'] > time()){ //即将开始
                $result_list[] = $rel_list;
            }
            if($tg_status == 3 && $group_info['tg_end_time'] < time()){ //已结束
                $result_list[] = $rel_list;
            }
            if($tg_status == 4 && $value['ksy_num'] <= 0){ //已售罄
                $result_list[] = $rel_list;
            }
            if($tg_status == 0){
                $result_list[] = $rel_list;
            }
        }
        return array_out(200, 'success', $result_list);
    }

    /**
     * 团购人员 列表
     * @author  shqiang
     * @return array_out(code, message, data)
     */
    public function group_sponsor_sku_userinfo($user_id, $store_id, $sponsor_id, $sku_id){
        $map = [];
        $map[] = ['sponsor_id', '=', $sponsor_id];
        $map[] = ['sku_id', '=', $sku_id];
        $list = model('ErpGroupTuxedo') -> where($map) -> order('id desc') -> select();
        $rel_list = [];
        foreach($list as $key => $value){
            //用户相关
            $userinfo = controller('user_center/Userlogic', 'logic\user') -> get_user_info($user_id, $user_id); //用户详情
            $userinfo = $userinfo['data'];
            $rel_list[$key]['userInfo'] = $userinfo;

            $goods_name = model('ErpGoodsSku') -> find($sku_id) -> goods_name;
            $rel_list[$key]['title'] = '成功团购' . $goods_name;
        }
        return array_out(200, 'success', $rel_list);
    }

    /**
     * 添加
     * @param tg_title 	                    string  团购标题
     * @param tg_join_num 		            int     参团人数
     * @param tg_status 		            int     状态 0禁用 1启用
     * @param tg_start_time 		        int     团购开始时间
     * @param tg_end_time 	                int     结束时间
     * @param group_sid 	                int     发起团购id
     * @param sku_id 		                int     单品id 参与的单品id
     * @param show_price 		            string  展示价格
     * @param limit_num 		            int     限购数量
     * @param sorts 	                    int     排序
     * @param sp_status 		            int     0禁用 1启用
     * @param tries 		                int     购买次数限制
     * @param sum_num 		                int     参与团购总库存数
     * @author dou<51779987@qq.com>
     * @return array_out(code, message, data)
     */
    public function add($user_id, $tg_title, $tg_join_num, $tg_status, $tg_start_time, $tg_end_time, $sku_info){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'	            =>  $user_id,
                'tg_title'		        =>  $tg_title,
                'tg_join_num'		    =>  $tg_join_num,
                'tg_start_time'	        =>  $tg_start_time,
                'tg_end_time'	        =>  $tg_end_time,
                'tg_status' 		    =>  $tg_status,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('ErpGroupSponsor') -> addData($tg_title, $tg_join_num, $tg_status, $tg_start_time, $tg_end_time);//添加
            if($rel['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');//添加失败 返回-1005,error
            }
            //插入团购商品表
            if(!empty($rel['data'])){
                foreach ($sku_info as $key => $value){
                    $sku_info[$key]['group_sid'] = $rel['data'];
                    $sku_info[$key]['sku_id'] = $key;
                    $sku_info[$key]['sp_status'] = intval(isset($value['sp_status'])?$value['sp_status']:1);
                    $sku_info[$key]['ksy_num'] = intval(isset($value['sum_num'])?$value['sum_num']:0);
                    unset($sku_info[$key]['id']);
                }
                $rel = model('ErpGroupSponsorSku') -> saveAll($sku_info);//添加
                if(!$rel){
                    DB::rollback();
                    return array_out(-1005, 'error2');//添加失败 返回-1005,error
                }
            }else{
                DB::rollback();
                return array_out(-1005, 'error3');//添加失败 返回-1005,error
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    public function edit($user_id, $id, $tg_title, $tg_join_num, $tg_status, $tg_start_time, $tg_end_time, $sku_info){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'	            =>  $user_id,
                'id'	                =>  $id,
                'tg_title'		        =>  $tg_title,
                'tg_join_num'		    =>  $tg_join_num,
                'tg_start_time'	        =>  $tg_start_time,
                'tg_end_time'	        =>  $tg_end_time,
                'tg_status' 		    =>  $tg_status,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('ErpGroupSponsor') -> editData($id, $tg_title, $tg_join_num, $tg_status, $tg_start_time, $tg_end_time);//添加
            if($rel['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');//添加失败 返回-1005,error
            }
            //插入团购商品表
            if(!empty($id)){
                foreach ($sku_info as $key => $value){
                    $sku_info[$key]['group_sid'] = $id;
                    $sku_info[$key]['sku_id'] = $key;
                    $sku_info[$key]['sp_status'] = intval(isset($value['sp_status'])?$value['sp_status']:1);
                }
                //没有新增已有更新
                foreach ($sku_info as $k=>$v){
                    if(!empty($v['id'])){
                        $maps['id'] = $id;
                        $rel = model('ErpGroupSponsorSku') ->update($v, $maps);
                    }else{
                        $rel = model('ErpGroupSponsorSku') ->isUpdate(false) ->data($v, true) -> save();
                    }
                }
                if(!$rel){
                    DB::rollback();
                    return array_out(-1005, 'error2');//添加失败 返回-1005,error
                }
            }else{
                DB::rollback();
                return array_out(-1005, 'error3');//添加失败 返回-1005,error
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    /**
     * 新  add & edit
     * 2020 10/13
     */
    public function add_goods($user_id, $group_sid, $sku_id, $show_price, $limit_num, $sorts, $sp_status, $tries, $sum_num, $dj_num, $ksy_num, $store_id, $data_list){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'	            =>  $user_id,
                'group_sid'		        =>  $group_sid,
                'sku_id'		        =>  $sku_id,
                'show_price'	        =>  $show_price,
                'limit_num'	            =>  $limit_num,
                'sorts' 		        =>  $sorts,
                'sp_status' 		    =>  $sp_status,
                'tries' 		        =>  $tries,
                'sum_num' 		        =>  $sum_num,
                'dj_num' 		        =>  $dj_num,
                'ksy_num' 		        =>  $ksy_num,
                'store_id' 		        =>  $store_id,
                'data_list' 		    =>  $data_list,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            if($store_id == 0){
                //查找所有门店
                $list_store_c = model('ErpStore')->order('create_time asc') -> column('id');
                if(!empty($list_store_c)){
                    foreach ($list_store_c as $key=>$value){
                        $rel = model('ErpGroupSponsorSku') ->addData($group_sid, $sku_id, $show_price, $limit_num, $sorts, $sp_status, $tries, $sum_num, $dj_num, $ksy_num, $value);//添加
                        if($rel['code'] < 0){
                            DB::rollback();
                            return array_out(-1005, 'error1');//添加失败 返回-1005,error
                        }
                        //插入图片表'group_skuid' => $id,
                        if(!empty($data_list)){
                            foreach ($data_list as $key => $value){
                                $data_list[$key]['group_skuid'] = $rel['data'];
                            }
                            $rel = model('ErpGroupSponsorSkuImgs') -> saveAll($data_list);//添加
                            if(!$rel){
                                DB::rollback();
                                return array_out(-1005, 'error4');//添加失败 返回-1005,error
                            }
                        }else{
                            DB::rollback();
                            return array_out(-1005, 'error5');//添加失败 返回-1005,error
                        }
                    }
                }else{
                    DB::rollback();
                    return array_out(-1005, 'error2');//添加失败 返回-1005,error
                }
            }else{
                $rel = model('ErpGroupSponsorSku') ->addData($group_sid, $sku_id, $show_price, $limit_num, $sorts, $sp_status, $tries, $sum_num, $dj_num, $ksy_num, $store_id);//添加
                if($rel['code'] < 0){
                    DB::rollback();
                    return array_out(-1005, 'error3');//添加失败 返回-1005,error
                }
                //插入图片表'group_skuid' => $id,
                if(!empty($data_list)){
                    foreach ($data_list as $key => $value){
                        $data_list[$key]['group_skuid'] = $rel['data'];
                    }
                    $rel = model('ErpGroupSponsorSkuImgs') -> saveAll($data_list);//添加
                    if(!$rel){
                        DB::rollback();
                        return array_out(-1005, 'error4');//添加失败 返回-1005,error
                    }
                }else{
                    DB::rollback();
                    return array_out(-1005, 'error5');//添加失败 返回-1005,error
                }
            }


            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

    public function edit_goods($user_id, $id, $group_sid, $store_id, $sku_id, $show_price, $limit_num, $sorts, $sp_status, $tries, $sum_num, $dj_num, $ksy_num, $data_list){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id'	            =>  $user_id,
                'id'	                =>  $id,
                'group_sid'		        =>  $group_sid,
                'sku_id'		        =>  $sku_id,
                'show_price'	        =>  $show_price,
                'limit_num'	            =>  $limit_num,
                'sorts' 		        =>  $sorts,
                'sp_status' 		    =>  $sp_status,
                'tries' 		        =>  $tries,
                'sum_num' 		        =>  $sum_num,
                'dj_num' 		        =>  $dj_num,
                'ksy_num' 		        =>  $ksy_num,
                'store_id' 		        =>  $store_id,
                'data_list' 		    =>  $data_list,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $rel = model('ErpGroupSponsorSku') ->editData($id, $group_sid, $sku_id, $show_price, $limit_num, $sorts, $sp_status, $tries, $sum_num, $dj_num, $ksy_num, $store_id);//添加
            if($rel['code'] < 0){
                DB::rollback();
                return array_out(-1005, 'error');//添加失败 返回-1005,error
            }
            //插入图片表
            if(!empty($data_list)){
                //先删除在插入
                $map = [];
                $map['group_sid'] = $group_sid;
                $map['group_skuid'] = $id;
                $map['sku_id'] = $sku_id;
                $rel_d = model('ErpGroupSponsorSkuImgs') -> where($map) -> delete();
                $rel = model('ErpGroupSponsorSkuImgs') -> saveAll($data_list);//添加
                if(!$rel){
                    DB::rollback();
                    return array_out(-1005, 'error2');//添加失败 返回-1005,error
                }
            }else{
                DB::rollback();
                return array_out(-1005, 'error3');//添加失败 返回-1005,error
            }

            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success');
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }
}